//
//  GengerViewController.m
//  YunGou
//
//  Created by macbook on 16/6/21.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "GengerViewController.h"

@interface GengerViewController ()
@property (nonatomic,strong)NSArray *sexArray;
@property (nonatomic,assign) NSInteger selectedRow;
@end

@implementation GengerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _sexArray = @[@"女",@"男"];
    _selectedRow = [_sexArray indexOfObject:[UserModel sharedInstance].gender];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    UILabel *sexLabel = [cell.contentView viewWithTag:1];
    sexLabel.text = _sexArray[indexPath.row];
    UIImageView *rightView = [cell.contentView viewWithTag:2];
    NSInteger gender = _selectedRow;
    if (gender == indexPath.row) {
        rightView.hidden = NO;
    }else{
        rightView.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == _selectedRow) {
        return;
    }
    UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedRow inSection:0]];
    UIImageView *lastImage = [lastCell.contentView viewWithTag:2];
    lastImage.hidden = YES;
    
    UITableViewCell *nowCell = [tableView cellForRowAtIndexPath:indexPath];
    UIImageView *nowImage = [nowCell.contentView viewWithTag:2];
    nowImage.hidden = YES;
    
    _selectedRow = indexPath.row;
    [self.tableView reloadData];
    [NetWorkTool updateUserInfoWithNickName:nil img:nil gender:[NSString stringWithFormat:@"%ld",_selectedRow] birth:nil qq:nil email:nil address:nil success:^(id object) {
        [[UserModel sharedInstance] setValue:[NSString stringWithFormat:@"%ld",indexPath.row] forKey:@"gender"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
